require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

ArenaRecordItem = {};
HelperFunc.SetUIEnv(ArenaRecordItem)
local this = ArenaRecordItem;

this.objHead = nil;
this.txtName = nil;
this.txtTime = nil;
this.txtScore = nil;
this.txtDiffScore = nil;
this.btnButton = nil;
this.imgCostIcon = nil;
this.freeController = nil;
this.challengeTicket = nil;

function ArenaRecordItem.Init()
    --todo 应该小写 暂时先大写
    local name, packageName = getUIName(PanelNames.UIArenaWinReward);
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI);
end

function ArenaRecordItem.onCreate()
    --obj:SetActive(false);
end

--- 初始化
--- @param obj GObject 节点
--- @param data BattleRecord 记录
function ArenaRecordItem.initData(obj, data, arena_type )
    objHead = obj:GetChild("headPlayer"):GetChild("head"):GetChild("HeadIcon");
    objFrame = obj:GetChild("headPlayer"):GetChild("frame");
    txtName = obj:GetChild("Name");
    txtTime = obj:GetChild("Time");
    txtScore = obj:GetChild("Score");
    txtDiffScore = obj:GetChild("ScoreDiff");
    scoreDiffController = obj:GetController("ScoreDiff");
    btnButton = obj:GetChild("Button");
    imgCostIcon = obj:GetChild("Button"):GetChild("icon");
    freeController = obj:GetController("isFree");
    challengeTicket = obj:GetChild("num");

    --- @type FighterInfo
    local myFighter;
    --- @type FighterInfo
    local otherFighter;
    -- 如果攻击方是自己，那么data.win = 1表示自己赢。如果攻击方不是自己，那么data.win = 0表示自己赢
    local win = 0;
    local myPlayerId = G.serverData["roleInfo"].show.playerId
    local attack_player_id
    if arena_type == EnumConst.ArenaBattleType.TeamCross then 
        if tostring(data.records[1].attacker.playerId) == tostring(myPlayerId) then
            myFighter = data.records[1].attacker;
            otherFighter = data.records[1].defender;
            win = data.records[1].win == 1 and 1 or 0;
        else
            myFighter = data.records[1].defender;
            otherFighter = data.records[1].attacker;
            win = data.records[1].win == 0 and 1 or 0;
        end
    else 
        if tostring(data.attacker.playerId) == tostring(myPlayerId) then
            myFighter = data.attacker;
            otherFighter = data.defender;
            win = data.win == 1 and 1 or 0;
        else
            myFighter = data.defender;
            otherFighter = data.attacker;
            win = data.win == 0 and 1 or 0;
        end
    end

    local arenaBaseInfo = logicMgr.ArenaManager.getBaseInfo();
    if arenaBaseInfo.times < arenaBaseInfo.freeTimes then
        freeController.selectedIndex = 0;
    else
        freeController.selectedIndex = 1;
        local infoChallengeTicketNum = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.ChallengeTicket);
        challengeTicket.text = infoChallengeTicketNum;
    end

    local headData = {
        icon = logicMgr.SkinManager.GetHeroIconBySkinId(otherFighter.pic),
        state = EnumConst.HeroHeadStateEnum.NONE;
    }
    if otherFighter.picFrame == 0 then
        otherFighter.picFrame = 800001
    end
    objHead.url =  headData.icon
    objFrame.url = "ui://ICONAtlas/" .. otherFighter.picFrame

    txtName.text = otherFighter.playerName
    local add_score
    local create_time
    if arena_type == EnumConst.ArenaBattleType.TeamCross then 
        add_score = data.records[1].addScore
        create_time = data.records[1].createTime
    else
        add_score = data.addScore
        create_time = data.createTime
    end
    if win == 1 then
        txtScore.text = tostring(myFighter.score - add_score)
        scoreDiffController.selectedIndex = 1
        txtDiffScore.text = "+" .. tostring(add_score)
        btnButton.visible = false
    else
        txtScore.text = tostring(myFighter.score + add_score)
        scoreDiffController.selectedIndex = 0
        txtDiffScore.text = "-" .. tostring(add_score)
        btnButton.visible = true
    end

    local diffTime = int64ToNumber(Config.serverTime - create_time) / 1000;
    if diffTime < 3600 then
        if diffTime < 60 then
            diffTime = 60;
        end
        txtTime.text = string.format("%s分钟前", math.floor(diffTime / 60));
    elseif diffTime < 86400 then
        txtTime.text = string.format("%s小时前", math.floor(diffTime / 3600));
    elseif diffTime < 2592000 then
        txtTime.text = string.format("%s天前", math.floor(diffTime / 86400));
    elseif diffTime < 31104000 then
        txtTime.text = string.format("%s月前", math.floor(diffTime / 2592000));
    else
        txtTime.text = "1年前";
    end

    local costIconName = logicMgr.ItemManager.getItemConstDatabyModelId(CurrencyEnum.ChallengeTicket).icon;
    --imgCostIcon.url = "ui://itemAtlas/" .. costIconName;
end

return ArenaRecordItem;
